package com.nuc6.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc6.entity.User;
import com.nuc6.service.UserService;
import com.nuc6.commons.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zwp
 * @since 2024-01-22
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/save")
    public Result saveUser(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Integer id){
        userService.removeById(id);
        return Result.success();
    }
    @RequestMapping("/list")
    public Result listUser(){
        List<User> users = userService.list();
        return Result.success(users);
    }
    @PutMapping("/update")
    public Result updateUser(@RequestBody User user){
        userService.updateById(user);
        return Result.success();
    }

    @GetMapping("/find")
    public Result<User> find(@RequestParam Integer id){
        User ids = userService.getById(id);
        return Result.success(ids);
    }
    @RequestMapping("/userByPage")
    public Result listUserByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "5") Integer pageSize,
                                 @RequestParam(defaultValue = "") String search){
        QueryWrapper<User> query = Wrappers.query();
        if(StringUtils.hasText(search)){
            query.like("identity",search);
        }
        Page<User> page = userService.page(new Page<>(pageNum, pageSize),query);
        return Result.success(page);
    }


}
